/*
Distributed under both the W3C Test Suite License [1] and the W3C
3-clause BSD License [2]. To contribute to a W3C Test Suite, see the
policies and contribution forms [3].

[1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license
[2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license
[3] http://www.w3.org/2004/10/27-testcases
*/

var A_05_03_01 = {
    name:'A_05_03_01',
    assert:'Retargeting focus events:' +
		'The focus, DOMFocusIn, blur, and DOMFocusOut events must be treated in the same way ' +
		'as events with a relatedTarget, where the corresponding node that is losing focus as ' +
		'a result of target gaining focus or the node that is gaining focus, and thus causing ' +
		'the blurring of target acts as the related target',
    highlight:'The focus, [[DOMFocusIn]], blur, and [[DOMFocusOut events must be treated in the same ' +
    	'way as events with a relatedTarget, where the corresponding node that is losing focus as ' +
    	'a result of target gaining focus or the node that is gaining focus, and thus causing ' +
    	'the blurring of target acts as the related target]]'
};

//blur and focus events are not bubbling. So this test tests only DOMFocusIn and DOMFocusOut
//which do bubble

//test DOMFocusOut event
var A_05_03_01_T01 = async_test('A_05_03_01_T01', PROPS(A_05_03_01, {
    author:'Sergey G. Grekhov <sgrekhov@unipro.ru>',
    reviewer:''
}));

A_05_03_01_T01.step(unit(function (ctx) {	
	
    var d = newRenderedHTMLDocument(ctx);
    
    var host = d.createElement('div');
    host.setAttribute('style', 'height:50%; width:100%');
    host.setAttribute('id', 'host');
    d.body.appendChild(host);
    
    //Shadow root to play with
    var s = createSR(host);

    var inp1 = d.createElement('input');
    inp1.setAttribute('id', 'inp1');
    inp1.setAttribute('type', 'checkbox');
    s.appendChild(inp1);
    
    var inp2 = d.createElement('input');
    inp2.setAttribute('id', 'inp2');
    inp2.setAttribute('type', 'checkbox');
    d.body.appendChild(inp2);
    
    s.addEventListener('DOMFocusOut', A_05_03_01_T01.step_func(function(event) {
    	assert_equals(event.target.getAttribute('id'), 'inp1', 'Inside shadow tree: Wrong target');  	
    }), false);
    
    d.body.addEventListener('DOMFocusOut', A_05_03_01_T01.step_func(function(event) {
    	assert_equals(event.target.getAttribute('id'), 'host', 'Inside shadow tree: Wrong target');  	
    }), false);
    
    inp1.focus();
    inp2.focus();
    
    A_05_03_01_T01.done();
}));


//test DOMFocusIn event
var A_05_03_01_T02 = async_test('A_05_03_01_T02', PROPS(A_05_03_01, {
    author:'Sergey G. Grekhov <sgrekhov@unipro.ru>',
    reviewer:''
}));

A_05_03_01_T02.step(unit(function (ctx) {	
	
    var d = newRenderedHTMLDocument(ctx);
    
    var host = d.createElement('div');
    host.setAttribute('style', 'height:50%; width:100%');
    host.setAttribute('id', 'host');
    d.body.appendChild(host);
    
    //Shadow root to play with
    var s = createSR(host);
    
    var inp1 = d.createElement('input');
    inp1.setAttribute('id', 'inp1');
    inp1.setAttribute('type', 'checkbox');
    s.appendChild(inp1);
    
    var inp2 = d.createElement('input');
    inp2.setAttribute('id', 'inp2');
    inp2.setAttribute('type', 'checkbox');
    d.body.appendChild(inp2);
    
    inp2.focus();
    
    s.addEventListener('DOMFocusIn', A_05_03_01_T02.step_func(function(event) {
    	assert_equals(event.target.getAttribute('id'), 'inp1', 'Inside shadoe tree: Wrong target');  	
    }), false);    
    
    d.body.addEventListener('DOMFocusIn', A_05_03_01_T02.step_func(function(event) {
    	assert_equals(event.target.getAttribute('id'), 'host', 'Outside shadow tree: Wrong target');  	
    }), false);
    
    inp1.focus();
    
    A_05_03_01_T02.done();
}));


//gaining and loosing focus elements are in the same tree.
//DOMFocusIn event should be stopped at shadow boundary
var A_05_03_01_T03 = async_test('A_05_03_01_T03', PROPS(A_05_03_01, {
    author:'Sergey G. Grekhov <sgrekhov@unipro.ru>',
    reviewer:''
}));


A_05_03_01_T03.step(unit(function (ctx) {	
	
    var d = newRenderedHTMLDocument(ctx);
    
    var host = d.createElement('div');
    host.setAttribute('style', 'height:50%; width:100%');
    host.setAttribute('id', 'host');
    d.body.appendChild(host);
    
    //Shadow root to play with
    var s = createSR(host);
    
    var inp1 = d.createElement('input');
    inp1.setAttribute('id', 'inp1');
    inp1.setAttribute('type', 'checkbox');
    s.appendChild(inp1);
    
    var inp2 = d.createElement('input');
    inp2.setAttribute('id', 'inp2');
    inp2.setAttribute('type', 'checkbox');
    s.appendChild(inp2);
    
    inp1.focus();
    
    d.body.addEventListener('DOMFocusIn', A_05_03_01_T03.step_func(function(event) {
    	assert_true(false, 'Event should be stopped at Shadow boundary');  	
    }), false);
    
    inp2.focus();
    
    A_05_03_01_T03.done();
}));




//gaining and loosing focus elements are in the same tree.
//DOMFocusOut event should be stopped at shadow boundary
var A_05_03_01_T04 = async_test('A_05_03_01_T04', PROPS(A_05_03_01, {
    author:'Sergey G. Grekhov <sgrekhov@unipro.ru>',
    reviewer:''
}));

A_05_03_01_T04.step(unit(function (ctx) {	
	
	 var d = newRenderedHTMLDocument(ctx);
	 
	 var host = d.createElement('div');
	 host.setAttribute('style', 'height:50%; width:100%');
	 host.setAttribute('id', 'host');
	 d.body.appendChild(host);
	 
	 //Shadow root to play with
	 var s = createSR(host);
	 
	 var inp1 = d.createElement('input');
	 inp1.setAttribute('id', 'inp1');
	 inp1.setAttribute('type', 'checkbox');
	 s.appendChild(inp1);
	 
	 var inp2 = d.createElement('input');
	 inp2.setAttribute('id', 'inp2');
	 inp2.setAttribute('type', 'checkbox');
	 s.appendChild(inp2);
	 
	 inp1.focus();
	 
	 d.body.addEventListener('DOMFocusOut', A_05_03_01_T04.step_func(function(event) {
	 	assert_true(false, 'Event should be stopped at Shadow boundary');  	
	 }), false);
	 
	 inp2.focus();
	 
	 A_05_03_01_T04.done();
}));




//Retargeting shouldn't occur for DOM tree nodes distributed
//among insertion point. Check DOMFocusOut
var A_05_03_01_T05 = async_test('A_05_03_01_T05', PROPS(A_05_03_01, {
  author:'Sergey G. Grekhov <sgrekhov@unipro.ru>',
  reviewer:''
}));

A_05_03_01_T05.step(unit(function (ctx) {	
	
	var d = newRenderedHTMLDocument(ctx);
	
	var host = d.createElement('div');
	host.setAttribute('id', 'host');
	d.body.appendChild(host);
	
	var inp1 = d.createElement('input');
	inp1.setAttribute('id', 'inp1');
	inp1.setAttribute('type', 'checkbox');
	inp1.setAttribute('class', 'clazz1');
	host.appendChild(inp1);
	
	var inp2 = d.createElement('input');
	inp2.setAttribute('id', 'inp2');
	inp2.setAttribute('type', 'checkbox');
	inp2.setAttribute('class', 'clazz2');
	host.appendChild(inp2);
	
	var inp3 = d.createElement('input');
	inp3.setAttribute('id', 'inp3');
	inp3.setAttribute('type', 'checkbox');
	inp3.setAttribute('class', 'clazz1');
	host.appendChild(inp3);
	
	
	//Shadow root to play with
	var s = createSR(host);
	
	var shadowDiv = document.createElement('div');
	shadowDiv.innerHTML = '<content select=".clazz1"></content>';
	s.appendChild(shadowDiv);
	
	//element outside the shadow tree
	var inp4 = d.createElement('input');
	inp4.setAttribute('id', 'inp4');
	inp4.setAttribute('type', 'checkbox');
	inp4.setAttribute('class', 'clazz1');
	d.body.appendChild(inp4);
	
	inp1.focus();
	
	s.addEventListener('DOMFocusOut', A_05_03_01_T05.step_func(function(event) {
		assert_equals(event.target.getAttribute('id'), 'inp1', 'Inside shadow tree: ' +
				'Event for nodes, distributed ' +
				'agains insertion points shouldn\'t be retargeted');  	
	}), false);
	
	
	d.body.addEventListener('DOMFocusOut', A_05_03_01_T05.step_func(function(event) {
		assert_equals(event.target.getAttribute('id'), 'inp1', 'Outside shadow tree: ' + 
				'Event for nodes, distributed ' +
				'agains insertion points shouldn\'t be retargeted');  	
	}), false);
	
	inp4.focus();
	
	A_05_03_01_T05.done();
}));


//Retargeting shouldn't occur for DOM tree nodes distributed
//among insertion points. Check DOMFocusIn
var A_05_03_01_T06 = async_test('A_05_03_01_T06', PROPS(A_05_03_01, {
	author:'Sergey G. Grekhov <sgrekhov@unipro.ru>',
	reviewer:''
}));

A_05_03_01_T06.step(unit(function (ctx) {	
	
	var d = newRenderedHTMLDocument(ctx);
	
	var host = d.createElement('div');
	host.setAttribute('id', 'host');
	d.body.appendChild(host);
	
	var inp1 = d.createElement('input');
	inp1.setAttribute('id', 'inp1');
	inp1.setAttribute('type', 'checkbox');
	inp1.setAttribute('class', 'clazz1');
	host.appendChild(inp1);
	
	var inp2 = d.createElement('input');
	inp2.setAttribute('id', 'inp2');
	inp2.setAttribute('type', 'checkbox');
	inp2.setAttribute('class', 'clazz2');
	host.appendChild(inp2);
	
	var inp3 = d.createElement('input');
	inp3.setAttribute('id', 'inp3');
	inp3.setAttribute('type', 'checkbox');
	inp3.setAttribute('class', 'clazz1');
	host.appendChild(inp3);
	
	
	//Shadow root to play with
	var s = createSR(host);
	
	var shadowDiv = document.createElement('div');
	shadowDiv.innerHTML = '<content select=".clazz1"></content>';
	s.appendChild(shadowDiv);
	
	//element outside the shadow tree
	var inp4 = d.createElement('input');
	inp4.setAttribute('id', 'inp4');
	inp4.setAttribute('type', 'checkbox');
	inp4.setAttribute('class', 'clazz1');
	d.body.appendChild(inp4);
	
	inp4.focus();
	
	s.addEventListener('DOMFocusIn', A_05_03_01_T06.step_func(function(event) {
		assert_equals(event.target.getAttribute('id'), 'inp1', 'Inside shadow tree: ' +
				'Event for nodes, distributed ' +
				'agains insertion points shouldn\'t be retargeted');  	
	}), false);
	
	
	d.body.addEventListener('DOMFocusIn', A_05_03_01_T05.step_func(function(event) {
		assert_equals(event.target.getAttribute('id'), 'inp1', 'Outside shadow tree: ' + 
				'Event for nodes, distributed ' +
				'agains insertion points shouldn\'t be retargeted');  	
	}), false);
	
	inp1.focus();
	
	A_05_03_01_T06.done();
}));
